package com.petshop.interceptor;

import com.petshop.utils.AdminHolder;
import com.petshop.utils.UserHolder;
import io.swagger.v3.oas.models.PathItem;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.HandlerInterceptor;

//import static com.petshop.utils.RedisConstant.userthreadLocal;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            //是options请求 直接放行
            return true;
        }
        // 判断是否需要拦截，也就是查看ThreadLocal是否有用户信息
        if (UserHolder.getUser() == null&&AdminHolder.getAmin() == null) {
            //没有用户信息，拦截
            response.setStatus(401);
            //拦截
            return false;
        }
        //有用户，放行
        return true;
    }
}
